﻿public class NetHandler_FetchUserInfos : GameServerMessageHandler
{
    public const string Key = "FetchUserInfos";

    public static NetHandler_FetchUserInfos NewHandler()
    {
        return new NetHandler_FetchUserInfos();
    }

    protected override ISerialize NewData() => new FetchUserInfosRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        FetchUserInfosRequestData requestData = _data as FetchUserInfosRequestData;

        GameDebug.Log($"用户{client.userID} 获取UserInfos:{requestData?.userIds.Length}");

        UserServer user = Game.instance.GetUser(client.userID);

        FetchUserInfoResponseData response = new FetchUserInfoResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                var uIds = requestData.userIds;

                UserInfo[] res = new UserInfo[uIds.Length];

                for (int i = 0; i < uIds.Length; i++)
                {
                    long deviceId = uIds[i];
                    if(deviceId > 0)
                        res[i] = UserServer.GetUserInfoWithUserId(deviceId);
                    
                    //users ut =  UserServer.FindInUserTable(deviceId);

                    //Tex icon = TextureSaver.LoadTexture(deviceId, 1);
                    //Tex tex1 = TextureSaver.LoadTexture(deviceId, 2);
                    //Tex tex2 = TextureSaver.LoadTexture(deviceId, 3);
                    //Tex tex3 = TextureSaver.LoadTexture(deviceId, 4);
                    //Tex tex4 = TextureSaver.LoadTexture(deviceId, 5);
                    //Tex tex5 = TextureSaver.LoadTexture(deviceId, 6);
                    //Tex tex6 = TextureSaver.LoadTexture(deviceId, 7);

                    //var userInfo = UserServer.UserTable2UserInfo(deviceId, ut, icon, tex1, tex2, tex3, tex4, tex5, tex6);

                    //res[i] = userInfo;
                }

                response.userInfos = res;

                response.code = 0;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
